package com.zmn.oms.admin.controller.report.collectionpayment;

import com.google.common.collect.Lists;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto.LayuiTableResponseDTO;
import com.zmn.mcc.cas.model.LoginStaff;
import com.zmn.mcc.cas.web.StaffAdminService;
import com.zmn.oms.admin.controller.OmsBaseController;
import com.zmn.oms.business.interfaces.report.ProviderPaymentBService;
import com.zmn.oms.common.vo.KVDict;
import com.zmn.oms.model.entity.report.ReportProviderPaymentQuery;
import com.zmn.oms.model.vo.report.ReportProviderPaymentVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 服务商/厂商收付款对账表controller
 *
 * @author: HuangYongJie
 * @version: v1.0
 * @since: 2019/11/7 16:04
 **/
@Controller
@Slf4j
@RequestMapping("/report/collectionpayment/")
public class ProviderPaymentListController extends OmsBaseController {
    /**
     * 是否保定下拉框
     */
    private static final List<KVDict> orderKeepSelectList = new ArrayList<>();
    private static final String SELECT_ORDER_KEEP = "orderKeep";

    /**
     * 厂商收款状态下拉框
     */
    private static final List<KVDict> customerBillStatusList = new ArrayList<>();
    private static final String SELECT_CUSTOMER_BILL_STATUS_STATUS = "customerBillStatus";
    /**
     * 服务商付款状态下拉框
     */
    private static final List<KVDict> partBillStatusList = new ArrayList<>();
    private static final String SELECT_PART_BILL_STATU = "partBillStatus";

    private static final List<KVDict> settlementTypeList = new ArrayList<>();
    private static final String SELECT_SETTLEMENT_TYPE = "settlementType";

    static {
        orderKeepSelectList.add(KVDict.builder().value(1).text("保外").build());
        orderKeepSelectList.add(KVDict.builder().value(2).text("保内").build());

        customerBillStatusList.add(KVDict.builder().value(100).text("待审核").build());
        customerBillStatusList.add(KVDict.builder().value(200).text("审核通过").build());
        customerBillStatusList.add(KVDict.builder().value(210).text("审核未过").build());
        customerBillStatusList.add(KVDict.builder().value(0).text("空").build());

        partBillStatusList.add(KVDict.builder().value(10).text("待审核").build());
        partBillStatusList.add(KVDict.builder().value(20).text("待付款").build());
        partBillStatusList.add(KVDict.builder().value(30).text("付款中").build());
        partBillStatusList.add(KVDict.builder().value(40).text("已付款").build());
        partBillStatusList.add(KVDict.builder().value(41).text("付款失败").build());
        partBillStatusList.add(KVDict.builder().value(0).text("空").build());

        settlementTypeList.add(KVDict.builder().value(1).text("预充值").build());
        settlementTypeList.add(KVDict.builder().value(2).text("后结算").build());
        settlementTypeList.add(KVDict.builder().value(0).text("空").build());

    }


    @Autowired
    private ProviderPaymentBService providerPaymentBService;

    @RequiresPermissions("/ys/oms/report/collectionpayment/provider/list.action")
    @RequestMapping("provider/list")
    public ModelAndView providerList(@RequestParam("plat") Integer plat) {
        ModelAndView mv = new ModelAndView();
        if (Objects.equals(GlobalConsts.PLAT_MARK_YEYX, plat)) {
            mv.setViewName("report/collectionpayment/provider/list");
        }
        if (Objects.equals(GlobalConsts.PLAT_MARK_ZMN, plat)) {
            mv.setViewName("report/collectionpayment/provider/list4zmn");
        }
        mv.addObject("plat", plat);
        return mv;
    }

    @RequiresPermissions("/ys/oms/report/collectionpayment/manufacturer/list.action")
    @RequestMapping("manufacturer/list")
    public ModelAndView manufacturerList(@RequestParam("plat") Integer plat) {
        ModelAndView mv = new ModelAndView();
        if (Objects.equals(GlobalConsts.PLAT_MARK_YEYX, plat)) {
            mv.setViewName("report/collectionpayment/manufacturer/list");
        }
        if (Objects.equals(GlobalConsts.PLAT_MARK_ZMN, plat)) {
            mv.setViewName("report/collectionpayment/manufacturer/list4zmn");
        }
        mv.addObject("plat", plat);
        return mv;
    }

    /**
     * 服务商/客户收付款对账表分页查询
     *
     * @param query
     * @return
     */
    @RequiresPermissions("/ys/oms/report/collectionpayment/provider/list.action")
    @ResponseBody
    @RequestMapping(value = "provider/loadData")
    public LayuiTableResponseDTO providerLoadData(ReportProviderPaymentQuery query) {
        List<ReportProviderPaymentVO> dataList = providerPaymentBService.listPageProviderPayment(query);
        LayuiTableResponseDTO<ReportProviderPaymentVO> success = LayuiTableResponseDTO.success(query.getTotalCount(), dataList);
        return success;
    }


    /**
     * 厂商/服务商收付款对账表分页查询
     *
     * @param query
     * @return
     */
    @RequiresPermissions("/ys/oms/report/collectionpayment/manufacturer/list.action")
    @ResponseBody
    @RequestMapping(value = "manufacturer/loadData")
    public LayuiTableResponseDTO manufacturerLoadData(ReportProviderPaymentQuery query) {
        query.setCustomerSettleType(3);
        List<ReportProviderPaymentVO> dataList = providerPaymentBService.listPageProviderPayment(query);
        LayuiTableResponseDTO<ReportProviderPaymentVO> success = LayuiTableResponseDTO.success(query.getTotalCount(), dataList);
        return success;
    }

    /**
     * 导出
     *
     * @param query
     * @return
     */
    @RequiresPermissions("/ys/oms/report/collectionpayment/manufacturer/list.action")
    @GetMapping(value = "manufacturer/export")
    public void manufacturerExport(ReportProviderPaymentQuery query, HttpServletResponse response) throws Exception {
        LoginStaff currentStaff = getCurrentStaff();
//        Integer exportLimit = currentStaff.getExportLimit();
        Integer exportLimit = StaffAdminService.currentStaff().getExportLimit();
        query.setPageSize(exportLimit);
        query.setCustomerSettleType(3);
        Workbook workBook = providerPaymentBService.exportExcel(query, true);
        workBook.write(response.getOutputStream());

    }

    /**
     * 导出
     *
     * @param query
     * @return
     */
    @RequiresPermissions("/ys/oms/report/collectionpayment/provider/list.action")
    @GetMapping(value = "provider/export")
    public void providerExport(ReportProviderPaymentQuery query, HttpServletResponse response) throws Exception {
        LoginStaff currentStaff = getCurrentStaff();
//        Integer exportLimit = currentStaff.getExportLimit();
        Integer exportLimit = StaffAdminService.currentStaff().getExportLimit();
        query.setPageSize(exportLimit);
        Workbook workBook = providerPaymentBService.exportExcel(query, false);
        workBook.write(response.getOutputStream());
    }

    /**
     * 下拉框
     *
     * @param type
     * @return
     */
    @GetMapping("getSelectData")
    @ResponseBody
    public List<KVDict> getSelectData(@RequestParam(defaultValue = "1") String type) {
        if (SELECT_ORDER_KEEP.equals(type)) {
            return orderKeepSelectList;
        } else if (SELECT_CUSTOMER_BILL_STATUS_STATUS.equals(type)) {
            return customerBillStatusList;
        } else if (SELECT_PART_BILL_STATU.equals(type)) {
            return partBillStatusList;
        } else if (SELECT_SETTLEMENT_TYPE.equals(type)) {
            return settlementTypeList;
        }
        return Lists.newArrayList();
    }

}
